<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class StatisticsDeviceDayEnergy extends Model
{
    //
    protected $fillable = ['device_id', 'site_id','user_id','value','date'];
    use SoftDeletes;

    public static function getOneDayByDeviceId($user_id,$device_id, $date)
    {
        $sdde = StatisticsDeviceDayEnergy::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->where('date', $date)->first();
        return $sdde;
    }

    public static function getSomeByDeviceId($user_id,$device_id, $start, $end)
    {
        $sddes = StatisticsDeviceDayEnergy::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('date', [$start, $end])->get();
        return $sddes;
    }

    public static function getOneMonthByDeviceId($user_id,$device_id, $month)
    {
        $begin = $month . '-01';
        $end = date('Y-m-d', strtotime('+1 month', strtotime($begin)));
        $sddes = StatisticsDeviceDayEnergy::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('date', [$begin, $end])->get();
        $total = 0;
        foreach ($sddes as $sdde) {
            $total = $total + $sdde->value;
        }
        return $total;
    }


}
